package com.wanghlme.bookstore.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanghlme.bookstore.mapper.UserinfoMapper;
import com.wanghlme.bookstore.service.UserinfoService;
import com.wanghlme.bookstore.entity.Userinfo;
import com.wanghlme.bookstore.util.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户信息业务实现类
 *
 * @author WangHailong
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo>
        implements UserinfoService {

    @Autowired
    private UserinfoMapper userinfoMapper;

    @Override
    public Userinfo signIn(String username, String password) {
        if (StringUtil.isNullEmptyBlank(username) || StringUtil.isNullEmptyBlank(password)) {
            return null;
        }
        return userinfoMapper.signIn(username, password);
    }

    @Override
    public Userinfo signUp(Userinfo userinfo) {
        if (userinfo == null) {
            return null;
        }
        String[] checks = new String[]{userinfo.getUsername(), userinfo.getEmail(), userinfo.getTelephone()};
        boolean isFit = true;
        for (String str : checks) {
            if (str != null) {
                Integer isExist = userinfoMapper.getUserinfoByAccount(str);
                if (isExist > 0) {
                    isFit = false;
                    break;
                }
            }
        }
        if (isFit) {
            Integer isSave = userinfoMapper.signUp(userinfo);
            if (isSave <= 0 || userinfo.getUserinfoId() == null) {
                return null;
            }
            return userinfoMapper.getUserinfoById(userinfo.getUserinfoId());
        } else {
            return null;
        }
    }

    @Override
    public Integer updateUserinfo(Userinfo userinfo) {
        if (userinfo == null) {
            return null;
        }
        if (StringUtil.isNullEmptyBlank(userinfo.getUserinfoId())) {
            return null;
        }
        if (StringUtil.isNullEmptyBlank(userinfo.getUsername())
                && StringUtil.isNullEmptyBlank(userinfo.getEmail())
                && StringUtil.isNullEmptyBlank(userinfo.getTelephone())) {
            return null;
        }
        return userinfoMapper.updateUserinfo(userinfo);

    }

    @Override
    public Userinfo getUserinfoById(String userId) {
        if (StringUtil.isNullEmptyBlank(userId)) {
            return null;
        } else {
            return userinfoMapper.getUserinfoById(userId);
        }
    }

    @Override
    public Integer getUserinfoByAccount(String originStr) {
        if (StringUtil.isNullEmptyBlank(originStr)) {
            return null;
        } else {
            return userinfoMapper.getUserinfoByAccount(originStr);
        }
    }
}




